import request from '@utils/request';
import * as actionTypes from './actionType'
import {
      message
} from 'antd';
//登陆
export const login = (payload) => {

      return (dispatch) => {
            request('POST', '/api/server/web/rest/user/login', {
                  cmd: "login",
                  type: "request",
                  request: {
                        ...payload.request
                  }
            }).then(obj => {
                  let res = obj.response;
                  localStorage.setItem("role",res.systemRole)

                  if (res.res) {
                        if (res.systemRole === "admin") {
                              window.location.href = '/Administrator/UserManagement'
                        } else {
                              window.location.href = '/Administrator/Lineinspector/AlarmHandling'
                        }
                       
                              sessionStorage.setItem('account',res.account)
                  
                        sessionStorage.setItem('role',res.systemRole)
                        message.success('登录成功', 1);
                        sessionStorage.setItem('islogin', JSON.stringify(res.res));
                        dispatch(Login())
                  } else {
                        message.error('登陆失败' + res.exception, 1);
                  }
            }).catch(err => console.log(err));
      }
}
const Login = (payload) => ({
      type: actionTypes.GETLOGIN,
      payload
})

export const isloginRequest = () => {
      const IP_URL = '/api/server/web/rest/user/isLogin'
      console.log(IP_URL, 'ip')
      return (dispatch) => {
            request("POST", IP_URL, {
                  cmd: "isLogin",
                  type: "request",
                  request: {

                  }
            }).then(response => {
                  const result = response.response;
                  if (result.res) {
                        sessionStorage.setItem(
                              "islogin",
                              JSON.stringify(result.res)
                        );
                        console.log(result.res, 'res')
                  } else {
                        sessionStorage.setItem("islogin", JSON.stringify(false));
                        window.location.href = '/login'
                  }

            }).catch(error => {
                  console.log(error)
            });
      };
};

//注销登录
export const logoutRequest = (params) => {
      // const IP_URL = process.env.REMOTE_SERVER_IP + params.api
      return (dispatch) => {
            request("POST", '/api/server/web/rest/user/logout', {
                  cmd: "logout",
                  type: "request",
                  request: {

                  }
            }).then(response => {
                  const result = response.response;
                  if (result.res) {
                        message.success('账号注销成功', 1);
                        sessionStorage.clear();
                        dispatch(logoutReducer(result.message))
                  } else {
                        message.error('账号注销失败' + result.exception, 1.5);
                  }
            }).catch(error => {
                  console.log(error)
            });
      };
};

//账号注销
const logoutReducer = (result) => ({
      type: actionTypes.LOGOUT,
      result
})
//获取最新二维码信息
export const getQRcode = () => {
      return (dispatch) => {
            request("POST", '/api/server/web/rest/updateApp/androidUpdateApp', {
                  cmd: "androidUpdateApp",
                  type: "request",
                  request: {
                  }
            }).then(response => {
                  console.log(response)
                  if (response.id != undefined) {
                        console.log('asdad')
                        dispatch(getqrcode(response))
                  } else {
                        message.error('获取二维码失败');
                  }
            }).catch(error => {
                  console.log(error)
            });
      };
}

//获取验证码
export const getCode = () => {
      return (dispatch) => {
            request("POST", '/api/server/web/rest/user/getCode', {
                  "cmd": "getCode",
                  "type": "request",
                  "request": {
                  }
            }).then(response => {
                  //console.log("111",response.response.res)
                  let result = response.response.message;
                  if (response.response.res) {
                        console.log('response=>', result)
                  }else{
                        message.error('获取验证码失败');
                  }
                  dispatch(getQcode(result))
            }).catch(err => {
                  console.log()
                  message.error('网络异常')
            });
      };
}
const getQcode = (validateString) => ({
      type: actionTypes.GETQCODE,
      validateString
})



const getqrcode = (payload) => ({
      type: actionTypes.GETQRCODE,
      payload
})